<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class Salesman extends \app\BaseModel
{
    protected $name = "massage_salesman_list";
    public function getTrueUserNameAttr($value, $data)
    {
        if(isset($value)) {
            if(!empty($value)) {
                return $value;
            }
            if(!empty($data["user_name"])) {
                return $data["user_name"];
            }
        }
    }
    public function getPhoneAttr($value, $data)
    {
        if(!empty($value) && isset($data["uniacid"]) && numberEncryption($data["uniacid"]) == 1) {
            return substr_replace($value, "****", 2, 4);
        }
        return $value;
    }
    public function getAdminIdAttr($value, $data)
    {
        if(!empty($value)) {
            $admin_model = new Admin();
            $admin = $admin_model->where(["id" => $value, "status" => 1, "salesman_auth" => 1])->count();
            return 0 < $admin ? $value : 0;
        }
        return 0;
    }
    public function getBalanceAttr($value, $data)
    {
        if(isset($value)) {
            return floatval($value);
        }
    }
    public function getInvChannelBalanceAttr($value, $data)
    {
        if(isset($value)) {
            return floatval($value);
        }
    }
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("status desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function adminDataList($dis, $mapor, $page = 10)
    {
        $data = $this->alias("a")->join("massage_service_user_list b", "a.user_id = b.id", "left")->where($dis)->where(function ($query) use ($mapor){
            $query->whereOr($mapor);
        })->field("a.*,b.nickName,b.avatarUrl")->group("a.id")->order("id desc")->paginate($page)->toArray();
        if(!empty($data["data"])) {
            $admin_model = new Admin();
            foreach ($data["data"] as &$v) {
                $admin = $admin_model->dataInfo(["id" => $v["admin_id"], "status" => 1]);
                if(!empty($admin["salesman_auth"])) {
                    $v["admin_name"] = $admin["agent_name"];
                } else {
                    $v["admin_id"] = 0;
                }
                if($v["balance"] < 0) {
                    $v["balance"] = getConfigSetting($v["uniacid"], "salesman_balance");
                }
            }
        }
        return $data;
    }
    public function salesmanOrderPrice($salesman_id, $channel_id = 0, $type = 1)
    {
        $order_model = new Order();
        $dis = ["pay_type" => 7, "salesman_id" => $salesman_id];
        if(!empty($channel_id)) {
            $dis["channel_id"] = $channel_id;
        }
        if($type == 1) {
            $price = $order_model->where($dis)->sum("true_service_price");
            return round($price, 2);
        }
        if($type == 3) {
            $price = $order_model->where($dis)->sum("material_price");
            return round($price, 2);
        }
        $order_id = $order_model->where($dis)->column("id");
        return $order_id;
    }
    public function getSalesmanChannelCash($salesman_id, $channel_id)
    {
        $order_id = $this->salesmanOrderPrice($salesman_id, $channel_id, 2);
        $comm_model = new Commission();
        $dis = ["type" => 12, "status" => 2];
        $cash = $comm_model->where("order_id", "in", $order_id)->where($dis)->sum("cash");
        return round($cash, 2);
    }
    public function getSalesmanCash($salesman_id)
    {
        $comm_model = new Commission();
        $dis = ["top_id" => $salesman_id, "status" => 2, "type" => 12];
        $cash = $comm_model->where($dis)->sum("cash");
        return round($cash, 2);
    }
    public function checkAuthData($data)
    {
        $cap_dis[] = ["user_id", "=", $data["id"]];
        $cap_dis[] = ["status", "in", [1, 2, 3, 4]];
        $cap_info = $this->where($cap_dis)->order("id desc")->find();
        $cap_info = !empty($cap_info) ? $cap_info->toArray() : [];
        $arr["salesman_status"] = !empty($cap_info) ? $cap_info["status"] : -1;
        $arr["salesman_sh_text"] = !empty($cap_info) ? $cap_info["sh_text"] : "";
        $arr["wallet_status"] = in_array($arr["salesman_status"], [2, 3]) ? 1 : 0;
        return $arr;
    }
}

?>